<?php

/**
 *
 * @author Administrator
 */
class AccountController {
	
	// TODO - Insert your code here
	private $path;
	private $lan;

	/**
	 */
	function __construct() {

		$this->path = "../src/module/admin/page_account/temp/";
		
		if (isset( $_SESSION[SESSION_LANGUAGE])) {
			$this->lan = $_SESSION[SESSION_LANGUAGE];
		} else {
			$this->lan = DEFAULT_LANGAUGE;
		}
	}

	/**
	 * View table page
	 * @return Ambigous <string, unknown_type, boolean>
	 */
	public function viewTalbe() {

		$template = new XTemplate( $this->path . $this->lan . "/table.html");
		
		// return page
		$template->parse( PAGECONTENT);
		return $template->out( PAGECONTENT);
	}

	/**
	 * View gird page
	 * @return Ambigous <string, unknown_type, boolean>
	 */
	public function viewGrid() {

		$template = new XTemplate( $this->path . $this->lan . "/grid.html");
		
		// assign controller and action
		$template->assign( CONTROLLER, PVALUE_CONTROLLER_PAGE_ACCOUNT);
		$template->assign( ACTION, PVALUE_ACTION_SUBMIT_DELETE_MANY);
		$template->assign( TYPE, $_REQUEST[PARAM_TYPE]);
		
		$db = new DBAccess();
		
		// check user type for select list user form data base
		if (isset( $_REQUEST[PARAM_TYPE]) && $_REQUEST[PARAM_TYPE] == USER_TYPE_MANAGER) { // in
		                                                                                           // case
		                                                                                           // type
		                                                                                           // is
		                                                                                           // manager
			
			if ($_SESSION[SESSION_ROLE] == USER_ROLE_SUPERADMIN) {
				$list1 = $db->selectManyByCause( $GLOBALS['userShortColArray'], USERS, array(USER_ROLE => USER_ROLE_ADMIN));
				$list2 = $db->selectManyByCause( $GLOBALS['userShortColArray'], USERS, array(USER_ROLE => USER_ROLE_MOD));
				$list = array_merge( $list1, $list2);
			} else if ($_SESSION[SESSION_ROLE] == USER_ROLE_ADMIN) {
				$list = $db->selectManyByCause( $GLOBALS['userShortColArray'], USERS, array(USER_ROLE => USER_ROLE_MOD));
			} else {
				$list = null;
			}
		
		} else { // in case type is end user
			
			$list = $db->selectManyByCause( $GLOBALS['userShortColArray'], USERS, array(USER_TYPE => USER_TYPE_ENDUSER));
		}
		
		// assign list
		foreach ( $list as $item ) {
			$template->assign( ITEM, $item);
			$template->parse( PAGECONTENT . "." . ITEM);
		}
		
		// return page
		$template->parse( PAGECONTENT);
		return $template->out( PAGECONTENT);
	}

	/**
	 * View sort page
	 * @return Ambigous <string, unknown_type, boolean>
	 */
	public function viewSort() {

		$template = new XTemplate( $this->path . $this->lan . "/table.html");
		
		// return page
		$template->parse( PAGECONTENT);
		return $template->out( PAGECONTENT);
	}

	/**
	 * View form page
	 * @return Ambigous <string, unknown_type, boolean>
	 */
	public function viewForm() {

		$template = new XTemplate( $this->path . $this->lan . "/form.html");
		
		// assign controller and action
		$template->assign( CONTROLLER, PVALUE_CONTROLLER_PAGE_ACCOUNT);
		$template->assign( ACTION, PVALUE_ACTION_SUBMIT_FORM);
		$template->assign( TYPE, $_REQUEST[PARAM_TYPE]);
		
		// get id
		if (isset( $_REQUEST[PARAM_ID]) && ! empty( $_REQUEST[PARAM_ID])) {
			$db = new DBAccess();
			$item = $db->selectOneByCause( $GLOBALS['userFullColArray'], USERS, array(USER_ID => $_REQUEST[PARAM_ID]));
		} else {
			$item = array();
			$item[USER_ROLE] = USER_ROLE_MOD;
			$item[USER_TYPE] = $_REQUEST[PARAM_TYPE];
		}
		
		if ($item[USER_TYPE] == USER_TYPE_MANAGER){
			$userRoleArray = $GLOBALS['userRoleArray'];
			if ($_SESSION[SESSION_ROLE] == USER_ROLE_SUPERADMIN) {
				unset( $userRoleArray[USER_ROLE_SUPERADMIN]);
			} else if ($_SESSION[SESSION_ROLE] == USER_ROLE_ADMIN) {
				unset( $userRoleArray[USER_ROLE_SUPERADMIN]);
				unset( $userRoleArray[USER_ROLE_ADMIN]);
			}
		} else {
			$userRoleArray = array(USER_TYPE_ENDUSER => "User khách hàng");
		}
		
		$item[USER_ROLE] = createSelectTagFormData( USER_ROLE, "full-width", $userRoleArray, $item[USER_ROLE]);
		
		// assign form item
		$template->assign( FORMITEM, $item);
		$template->parse( PAGECONTENT . "." . FORMITEM);
		
		// return page
		$template->parse( PAGECONTENT);
		return $template->out( PAGECONTENT);
	}

	/**
	 * submit form function
	 * @return multitype:
	 */
	public function submitForm() {
		
		// reponse array
		$response_array = array();
		
		// get input
		$item = array();
		$item[USER_ID] = $_REQUEST[USER_ID];
		
		$item[USER_USERNAME] = $_REQUEST[USER_USERNAME];
		$item[USER_PASSWORD] = md5( "1234567890");
		$item[USER_EMAIL] = $_REQUEST[USER_EMAIL];
		$item[USER_ROLE] = $_REQUEST[USER_ROLE];
		$item[USER_TYPE] = $_REQUEST[USER_TYPE];
		
		$item[USER_FULLNAME] = $_REQUEST[USER_FULLNAME];
		$item[USER_IMAGE] = $_REQUEST[USER_IMAGE];
		$item[USER_CARDID] = $_REQUEST[USER_CARDID];
		$item[USER_DATEOFBIRTH] = $_REQUEST[USER_DATEOFBIRTH];
		$item[USER_ADDRESS] = $_REQUEST[USER_ADDRESS];
		$item[USER_CELLPHONE] = $_REQUEST[USER_CELLPHONE];
		$item[USER_HOMEPHONE] = $_REQUEST[USER_HOMEPHONE];
		
		// update data to database
		$db = new DBAccess();
		
		if ($item[USER_ID] == 0) {
			if ($db->insertData( $item, USERS)) {
				$response_array[STATUS] = STATUS_OK;
				$response_array[MESSAGE] = getMessage( MESSAGE_INSERT_SUCCESSFUL);
				$response_array[BACK] = "maincontent.php?ctrl=" . PVALUE_CONTROLLER_PAGE_ACCOUNT . "&act=" . PVALUE_ACTION_VIEW_GRID;
			} else {
				$response_array[STATUS] = STATUS_FAIL;
				$response_array[MESSAGE] = getMessage( MESSAGE_INSERT_FAIL);
			}
		} else {
			if ($db->updateData( $item, USERS)) {
				$response_array[STATUS] = STATUS_OK;
				$response_array[MESSAGE] = getMessage( MESSAGE_UPDATE_SUCCESSFUL);
				$response_array[BACK] = "maincontent.php?ctrl=" . PVALUE_CONTROLLER_PAGE_ACCOUNT . "&act=" . PVALUE_ACTION_VIEW_GRID;
			} else {
				$response_array[STATUS] = STATUS_FAIL;
				$response_array[MESSAGE] = getMessage( MESSAGE_UPDATE_FAIL);
			}
		}
		
		// return repnose array;
		return $response_array;
	}

	/**
	 * submit form function
	 * @return multitype:
	 */
	public function submitSort() {
		
		// reponse array
		$response_array = array();
		
		// return repnose array;
		return $response_array;
	}

	/**
	 * submit form function
	 * @return multitype:
	 */
	public function submitDeleteOne() {
		
		// reponse array
		$response_array = array();
		
		if (isset( $_REQUEST[PARAM_ID]) && ! empty( $_REQUEST[PARAM_ID])) {
			
			$db = new DBAccess();
			
			if ($db->deleteData( USERS, $_REQUEST[PARAM_ID])) {
				$response_array[STATUS] = STATUS_OK;
				$response_array[MESSAGE] = MESSAGE_DELETE_SUCCESSUL;
			} else {
				$response_array[STATUS] = STATUS_FAIL;
				$response_array[MESSAGE] = MESSAGE_DELETE_FAIL;
			}
		} else {
			$response_array[STATUS] = STATUS_FAIL;
			$response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
		}
		
		// return repnose array;
		return $response_array;
	}

	/**
	 * submit form function
	 * @return multitype:
	 */
	public function submitDelteMany() {
		
		// reponse array
		$response_array = array();
		
		if (isset( $_REQUEST[PARAM_SELECTED])) {
			
			$IDArray = $_REQUEST[PARAM_SELECTED];
			
			if ($IDArray != null && count( $IDArray) > 0) {
				$delete = true;
				$db = new DBAccess();
				foreach ( $IDArray as $id ) {
					if ($db->deleteData( USERS, $id)) {
					} else {
						$delete = false;
					}
				}
				
				if ($delete) {
					$response_array[STATUS] = STATUS_OK;
					$response_array[MESSAGE] = MESSAGE_DELETE_SUCCESSUL;
				} else {
					$response_array[STATUS] = STATUS_FAIL;
					$response_array[MESSAGE] = MESSAGE_DELETE_FAIL;
				}
			} else {
				$response_array[STATUS] = STATUS_ERROR;
				$response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
			}
		} else {
			$response_array[STATUS] = STATUS_ERROR;
			$response_array[MESSAGE] = MESSAGE_DELETE_NOT_ID;
		}
		
		// return repnose array;
		return $response_array;
	}

	/**
	 */
	function __destruct() {
		
		// TODO - Insert your code here
	}
}

?>